/**
 * Copyright 2013 Douglas Lacher
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @author Douglas Lacher <doug.lacher@gmail.com>
 */

package com.devioustoast.andmoco;

import android.util.Log;

/**
 * A movement represents a single servo and its new position.
 *
 * @author Douglas Lacher <doug.lacher@gmail.com>
 *
 */
public class Movement implements Runnable {

  public static final String LOG_TAG = "Movement";

  /**
   * A reference to the Servo object to move.
   */
  public Servo servo = null;
  /**
   * The relative or absolute position to which to move the Servo in microseconds pulse-width.  May
   * be a positive or negative value.  Must not be used with degrees.
   */
  public Integer microseconds = null;
  /**
   * The relative or absolute position to which to move the Servo in degrees.  May be positive or
   * negative.  Must not be used with microseconds.
   */
  public Float degrees = null;
  /**
   * Set true to add the servo's current position to the new position.  Set false to treat the new
   * position as an absolute value without regard for the servo's current position.
   */
  public Boolean relative = null;
  /**
   * The amount of time in milliseconds to wait after sending this movement command.
   */
  public Integer waitTimeMs = null;

  /**
   * @param servo the servo number.
   * @param microseconds the new position to which the servo should move.  Position is measured in
   *        micro-seconds (as a pulse-width) and has a range dependent on the servos being used.
   * @param relative set true to treat the new position as a relative position.  Set false to treat
   *        it as an absolute position.
   * @param waitTimeMs time in milliseconds to wait after this move.  This is in addition to the
   *        small pause between sending commands.
   */
  public Movement(Servo servo, Integer position, Boolean relative, Integer waitTimeMs) {
    this.servo = servo;
    this.microseconds = position;
    this.relative = relative;
    this.waitTimeMs = waitTimeMs;
  }

  /**
   * @param servo the servo number.
   * @param degrees the new position to where the servo should move.  Position is measured
   *        in degrees.
   * @param relative set true to treat the new position as a relative position.  Set false to treat
   *        it as an absolute position.
   * @param waitTimeMs time in milliseconds to wait after this move.  This is in addition to the
   *        small pause between sending commands.
   */
  public Movement(Servo servo, Float degrees, Boolean relative, Integer waitTimeMs) {
    this.servo = servo;
    this.degrees = degrees;
    this.relative = relative;
    this.waitTimeMs = waitTimeMs;
  }

  /**
   * Equivalent to Movement(servo, microseconds, relative, 0).
   *
   * @param servo the servo number.
   * @param microseconds the new position to which the servo should move.  Position is measured in
   *        micro-seconds (as a pulse-width) and has a range dependent on the servos being used.
   * @param relative set true to treat the new position as a relative position.  Set false to treat
   *        it as an absolute position.
   */
  public Movement(Servo servo, Integer position, Boolean relative) {
    this.servo = servo;
    this.microseconds = position;
    this.relative = relative;
  }

  /**
   * Equivalent to Movement(servo, degrees, relative, 0).
   *
   * @param servo the servo number.
   * @param degrees the new position to where the servo should move.  Position is measured
   *        in degrees.
   * @param relative set true to treat the new position as a relative position.  Set false to treat
   *        it as an absolute position.
   */
  public Movement(Servo servo, Float degrees, Boolean relative) {
    this.servo = servo;
    this.degrees = degrees;
    this.relative = relative;
  }

  /**
   * Creates a Movement that does nothing except wait the specified time.
   *
   * @param waitTimeMs time to wait in milliseconds.
   */
  public Movement(Integer waitTimeMs) {
    this.waitTimeMs = waitTimeMs;
  }

  /**
   * Do not call this constructor.  This exists only to make subclassing easier.
   */
  public Movement() { }

  @Override
  public String toString() {
    Integer servoNumber = null;
    if (servo != null) {
      servoNumber = servo.getServoNumber();
    }
    return String.format(
        "Servo %s : degrees %s : microseconds %s : relative %s : wait time %s",
        servoNumber, degrees, microseconds, relative, waitTimeMs);
  }

  /**
   * Runs this movement.
   */
  public void run() {
    try {
      if (servo != null) {
        if (relative) {
          if (microseconds != null) {
            servo.setRelativePositionUsec(microseconds);
          } else if (degrees != null) {
            servo.setRelativePositionDegrees(degrees);
          }
        } else {
          if (microseconds != null) {
            servo.setAbsolutePositionUsec(microseconds);
          } else if (degrees != null) {
            servo.setAbsolutePositionDegrees(degrees);
          }
        }
      }
      if (waitTimeMs != null && waitTimeMs > 0) {
        try {
          Thread.sleep(waitTimeMs);
        } catch (InterruptedException e) {
          Log.d(LOG_TAG, "Sleep interrupted.", e);
        }
      }
    } catch (InterruptedException e) {
      Log.e(LOG_TAG, "Unable to run movement.", e);
    }
  }
}
